const sql = require('mssql');
const baseFn = require('../../public/base.js');

/**
 * @description 入库拒收审核
 */


//  获得订单信息

const getVerifyStorageRefuse = async (ctx, next) => {
    try {
        const { index, pageSize, searchList } = ctx.request.body;
        console.log('searchList', searchList);
        let publicSql = ` FROM GSP_YPJSBGD GS LEFT JOIN BM_WLDW DW ON GS.DWBM = DW.BM LEFT JOIN BM_YP Y ON GS.YPBM = Y.BM WHERE GS.BMBM = '${searchList.BMBM}' `;
        let searchSqlList = [];
        let totalSql = ` SELECT COUNT(GS.ID) AS NUM ${publicSql} `;
        let selectSql = `SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY GS.ID) AS px, GS.ID, GS.BMBM, GS.BMMC, GS.YPBM, GS.TYMC, GS.YPMC, GS.JXMC, GS.YPGG, GS.PZWH, GS.SCCJ, GS.GJQY, GS.JLDW, GS.SL, 
    GS.SCPH, GS.YXQZ, GS.JSYY, GS.YJ1, GS.RQ1, GS.QZ1, GS.YJ2, GS.RQ2, GS.QZ2, GS.YJ3, GS.RQ3, GS.QZ3, GS.CZY, GS.DJ, GS.JE, GS.JSLX, GS.DWBM, 
    GS.CGYMC, GS.JSRMC, GS.THFS, GS.YFJS, GS.WLGS, GS.WLDH, GS.ID_JSD ${publicSql} `;

        if (searchList.GYS) {
            searchSqlList.push(` AND (DW.BM LIKE '%${searchList.GYS}%' OR DW.MC LIKE '%${searchList.GYS}%' OR DW.JP LIKE '%${searchList.GYS}%' ) `);
        }

        if (searchList.YPBM) {
            searchSqlList.push(` AND (Y.BM LIKE '%${searchList.YPBM}%' OR Y.MC LIKE '%${searchList.YPBM}%' OR Y.JP LIKE '%${searchList.YPBM}%' ) `);
        }
        if (searchList.RQ[0]) {
            searchSqlList.push(` AND GS.RQ >= '${searchList.RQ[0]} 00:00:00' AND GS.RQ <= '${searchList.RQ[1]} 23:59:59' `);
        }

        totalSql += ` ${searchSqlList.join(' ')} `;

        selectSql += ` ${searchSqlList.join(' ')} ) AS t1 WHERE t1.px between ${(index - 1) * pageSize + 1} and ${index * pageSize} `

        let totalResult = await sql.query(totalSql);

        let selectResult = await sql.query(selectSql);

        ctx.body = { success: true, total: totalResult.recordset[0].NUM, result: selectResult.recordset }
    } catch (err) {
        ctx.body = { success: false, err }
    }

}

//  拒收审核

const keepVerifyStorageRefuse = async (ctx, next) => {
    // try {
    console.log(ctx.request.body);
    let body = ctx.request.body;
    const { id, type, BZ, SPR } = body;
    // check为验收部审核，business为业务部审核，quality为质管部审核
    let index = type === 'check' ? 1 : type === 'business' ? 2 : 3;
    let updateSql = ` UPDATE GSP_YPJSBGD SET RQ${index} = '${baseFn.getNowTimeId().fullDate}',YJ${index} = '${BZ}',QZ${index} = '${SPR}'  `;
    if ('logisticsInfo' in body) {
        let { YFJS, WLGS, WLDH } = body.logisticsInfo;
        updateSql += ` ,YFJS = '${YFJS}',WLGS = '${WLGS}',WLDH = '${WLDH}' `;
    }
    updateSql += ` WHERE ID = ${id} `;
    let updateResult = await sql.query(updateSql);
    if (updateResult.rowsAffected[0] > 0) {
        ctx.body = { success: true }
    } else {
        ctx.body = { success: false }
    }
    // } catch (err) {
    //     ctx.body = { success: false, err }
    // }

}





module.exports = {
    'POST /getVerifyStorageRefuse': getVerifyStorageRefuse,
    'POST /keepVerifyStorageRefuse': keepVerifyStorageRefuse,
    // 'POST /keepVeriryStoreSalesPurchaseInfo': keepVeriryStoreSalesPurchaseInfo
}
